<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;

class MakecontControl extends Controller
{
    //
    public function makeinfo (Request $request) {
      $cont = $_GET['cont'];
      $date = date('m月d日 h:i',time());
      $city = $_GET['city'];
      //动态数据
      $id = DB::table('dynamic')->insertGetId(
        ['d_id'=>session('userid'),'d_time'=>$date,'d_equi'=>$city]
      );
     $resource = DB::table('dycont')->insertGetId(
            ['dy_id'=>$id,'dy_cont'=>$cont]
        );
      if(!empty($resource)) {
          $newcont = array();
          $newcont['id'] = $id;
          $newcont['d_id'] = session('userid');
          $newcont['d_type'] = 1;
          $newcont['d_time'] = $date;
          $newcont['d_cont'] = $cont;
          $newcont['username'] = DB::table('user')->where('id',session('userid'))->value('u_name');
          $newcont['userimg'] = DB::table('user')->where('id',session('userid'))->value('u_img');
          $newcont['city'] = $city;
          $newcont['like'] = 0;
          return response()->json($newcont);
      }
      //
      //return view('welcome',['a'=>$cont]);                                    ;
    }
    //评论插入模块
    //评论插入模块
    public function insertpl (Request $request) {
        $content = $_GET['content'];
        $id = $_GET['id'];
        $date = date('m月d日 h:i',time());
        //动态数据
        $pid = DB::table('dycomment')->insertGetId(
            ['dc_id'=>$id,'dc_pid'=>session('userid'),'dc_time'=>$date,'dc_dz'=>0]
        );
        $resource = DB::table('plcont')->insertGetId(
            ['pl_id'=>$pid,'pl_cont'=>$content]
        );
        if(!empty($resource)) {
            $array = array();
            $array['uid'] = session('userid');
            $array['content'] = $content;
            $array['time'] = $date;
            $array['plid'] = $pid;
            $array['img'] = DB::table('user')->where('id',session('userid'))->value('u_img');
            $array['username'] = DB::table('user')->where('id',session('userid'))->value('u_name');

            return response()->json($array);
        }

    }
    //一级点赞模块
    public function oneinfo(Request $request){
        $id = $_GET['id'];
        $info = DB::table('onepl')->where('pl_id',$id)->where('uid',session('userid'))->get();

        return get_object_vars($info);
        if(empty(get_object_vars($info))){
            $resou = DB::table('onepl')->insertGetId(
                ['pl_id'=>$id,'uid'=>session('userid')]
            );
      }else {
            return 1;
           $del =  DB::table('onepl')->where('pl_id', '=', $id)->where('uid','=',session('userid'))->delete();

        }
//         if($resou || $del){
//
//            return 1;
//         }


    }

    //动态点赞模块
    public function dydz (Request $request){
        $id = $_GET['id'];
        $result = DB::table('dydz')->where('dy_id',$id)->where('uid',session('userid'))->first();
        if(empty($result)){
            DB::table('dydz')->insertGetId(
                ['dy_id'=>$id,'uid'=>session('userid')]
            );
            return 1;
        } else {
            DB::table('dydz')->where('dy_id', '=', $id)->where('uid','=',session('userid'))->delete();
        }
        return 2;
    }
    //动态收藏按钮
    public function dysci(){
        $id = $_GET['sid'];
        $result = DB::table('msc')->where('dy_id',$id)->where('uid',session('userid'))->first();

        if(empty($result)){
            DB::table('msc')->insertGetId(
                ['dy_id'=>$id,'uid'=>session('userid')]
            );
            return 1;
        } else {
            DB::table('msc')->where('dy_id', '=', $id)->where('uid','=',session('userid'))->delete();
            return 2;
        }
    }

    //关注插入
    public function mkgz (Request $request){
        $gid = $_GET['zid'];
        $type = $_GET['type'];
        if($type == 100) {
            $del =  DB::table('attention')->where('at_uid', '=', session('userid'))->where('at_gid','=',$gid)->delete();
            if(!empty($del)) {
                return 1;
            }
        } else {

            $id = DB::table('attention')->insertGetId(
                ['at_uid'=>session('userid'),'at_gid'=>$gid,'at_group'=>1]
            );

            if(!empty($id)){
                return 1;
            }
        }


    }

    //举报管理
    public function contre(Request $request) {
        $dyid = $_GET['id'];
        $resour = DB::table('dynamic')->where('id',$dyid)->first();
        $resour->cont = DB::table('dycont')->where('dy_id',$resour->id)->value('dy_cont');
        $resour->username = DB::table('user')->where('id',$resour->d_id)->value('u_name');
        $resour->img = DB::table('user')->where('id',$resour->d_id)->value('u_img');
        //return var_dump($resour);
        return view('home/content/reige',['resour'=>$resour]);
    }


    //举报ajax系统
    public function jucont (Request $request) {
        $jid = $_GET['jid'];

        $resour = DB::table('jbcont')->insertGetId(
            ['j_id'=>$jid,'uid'=>session('userid')]
        );
        if(!empty($resour)) {
            return $resour;
        }

    }
    //装发系统
    public function myzfi (Request $request){
        $zid = $_GET['zid'];
        $cont = $_GET['cont'];
        $date = date('m月d日 h:i',time());
        $id = DB::table('dynamic')->insertGetId(
            ['d_id'=>session('userid'),'d_type'=>2,'d_equi'=>'中国上海市','d_time'=>$date]
        );
        DB::table('dycont')->insertGetId(
            ['dy_id'=>$id,'dy_cont'=>$cont]
        );
        $res = DB::table('uzf')->insertGetId(
            ['ro_id'=>session('userid'),'ro_rid'=>$id]
        );
        if(!empty($res)) {
            $data = array();
            $data['id'] = $id;
            $data['cont'] = $cont;
            $data['d_type'] = 2;
            $data['username'] = DB::table('user')->where('id',session('userid'))->value('u_name');
            $data['userimg'] = DB::table('user')->where('id',session('userid'))->value('u_img');
            $data['zf'] = DB::table('dynamic')->where('id',$zid)->first();
            $data['zfcont'] = DB::table('dycont')->where('dy_id',$data['zf']->id)->value('dy_cont');
            $data['zfname'] =  DB::table('user')->where('id',$data['zf']->d_id)->value('u_name');
            $data['zfimg'] = DB::table('user')->where('id',$data['zf']->d_id)->value('u_img');
            $data['d_time'] = $date;
            $data['city'] = '中国上海市';
            $data['like'] = 0;

            return response()->json($data);
        }

    }

    //转发信息系统
    public function zinfo (Request $request) {
        $zid = $_GET['zid'];
        $result = DB::table('dynamic')->where('id',$zid)->first();
        $result->username = DB::table('user')->where('id',$result->d_id)->value('u_name');                  $result->userimg = DB::table('user')->where('id',$result->d_id)->value('u_img');
        $result->cont = DB::table('dycont')->where('dy_id',$result->id)->value('dy_cont');
        return  response()->json($result);

    }

}
